Telegram Group & Telegram Channel
🐼 Задача на знание Pandas

Условие: У вас есть датафрейм df, содержащий два столбца: A и B. Столбец A содержит даты, а столбец B — числовые значения. Как бы вы рассчитали скользящее среднее по столбцу B с окном в 3 дня, предполагая, что значения в столбце A идут подряд и без пропусков?

Решение:
import pandas as pd

data = {
'A': ['2023-01-01', '2023-01-02', '2023-01-03', '2023-01-04', '2023-01-05',
'2023-01-06', '2023-01-07', '2023-01-08', '2023-01-09'],
'B': [18, 71, 13, 57, 7, 8, 69, 83, 27]
}

df = pd.DataFrame(data)

# Убедимся, что столбец A распознан как дата
df['A'] = pd.to_datetime(df['A'])

# Установим столбец дат как индекс датафрейма
df = df.set_index('A')

# Рассчитаем скользящее среднее по столбцу B с окном в 3 дня
df['Moving_Average'] = df['B'].rolling(window=3).mean()

print(df)


#python
#pandas
#программирование
Please open Telegram to view this post
VIEW IN TELEGRAM



tg-me.com/ds_interview_lib/247
Create:
Last Update:

🐼 Задача на знание Pandas

Условие: У вас есть датафрейм df, содержащий два столбца: A и B. Столбец A содержит даты, а столбец B — числовые значения. Как бы вы рассчитали скользящее среднее по столбцу B с окном в 3 дня, предполагая, что значения в столбце A идут подряд и без пропусков?

Решение:

import pandas as pd

data = {
'A': ['2023-01-01', '2023-01-02', '2023-01-03', '2023-01-04', '2023-01-05',
'2023-01-06', '2023-01-07', '2023-01-08', '2023-01-09'],
'B': [18, 71, 13, 57, 7, 8, 69, 83, 27]
}

df = pd.DataFrame(data)

# Убедимся, что столбец A распознан как дата
df['A'] = pd.to_datetime(df['A'])

# Установим столбец дат как индекс датафрейма
df = df.set_index('A')

# Рассчитаем скользящее среднее по столбцу B с окном в 3 дня
df['Moving_Average'] = df['B'].rolling(window=3).mean()

print(df)


#python
#pandas
#программирование

BY Библиотека собеса по Data Science | вопросы с собеседований


Warning: Undefined variable $i in /var/www/tg-me/post.php on line 283

Share with your friend now:
tg-me.com/ds_interview_lib/247

View MORE
Open in Telegram


Библиотека собеса по Data Science | вопросы с собеседований Telegram | DID YOU KNOW?

Date: |

The SSE was the first modern stock exchange to open in China, with trading commencing in 1990. It has now grown to become the largest stock exchange in Asia and the third-largest in the world by market capitalization, which stood at RMB 50.6 trillion (US$7.8 trillion) as of September 2021. Stocks (both A-shares and B-shares), bonds, funds, and derivatives are traded on the exchange. The SEE has two trading boards, the Main Board and the Science and Technology Innovation Board, the latter more commonly known as the STAR Market. The Main Board mainly hosts large, well-established Chinese companies and lists both A-shares and B-shares.

The Singapore stock market has alternated between positive and negative finishes through the last five trading days since the end of the two-day winning streak in which it had added more than a dozen points or 0.4 percent. The Straits Times Index now sits just above the 3,060-point plateau and it's likely to see a narrow trading range on Monday.

Библиотека собеса по Data Science | вопросы с собеседований from it


Telegram Библиотека собеса по Data Science | вопросы с собеседований
FROM USA